import loginApi from './../../api/login'
import { getToken, setToken, setUser, getUser } from './../../utils/auth'
// import { setToken, setUser } from './../../utils/auth'

const user = {
  state: {
    token: getToken(),
    user: getUser()
  },
  mutations: {
    SET_TOKEN (state, token) {
      state.token = token
      setToken(token)
    },
    SET_USER (state, user) {
      state.user = user
      setUser(user)
    }
  },
  actions: {
    // 登录
    Login ({ commit }, form) {
      return new Promise((resolve, reject) => {
        loginApi.login(form).then(response => {
          console.log(response)
          const resp = response.data
          commit('SET_TOKEN', resp.data.token)
          commit('SET_USER', resp.data)
          resolve(resp)
        }).catch(error => {
          reject(error)
        })
      })
    }
  }
}
export default user
